package com.xc.quickstart.manager.sso;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xc.quickstart.admin.query.LogoutQuery;
import com.xc.quickstart.admin.query.SSOQuery;
import com.xc.quickstart.manager.sso.model.FeignSSOResponseModel;
import com.xc.quickstart.manager.sso.model.SSOSimpleUserInfo;
import com.xc.quickstart.manager.sso.model.UserAuthorityModel;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.Decoder;
import feign.gson.GsonDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@FeignClient(value = "adminSSOClient", url = "${sso.root_path}", configuration = AdminSSOClient.SSOClientConfig.class)
interface AdminSSOClient {


    @Slf4j
    class SSOClientConfig implements RequestInterceptor {

        @Value("${sso.admin_app_id}")
        private String apiAppId;

        @Value("${sso.admin_app_secret}")
        private String apiAppSecret;

        @Bean
        public Decoder decoder() {
            //配置json 的解码器,使用gson的解码器
            return new GsonDecoder();
        }

        @Bean
        //配置打印日志
        public feign.Logger.Level multipartLoggerLevel() {
            return feign.Logger.Level.FULL;
        }

        @Override
        public void apply(RequestTemplate requestTemplate) {
            String method = requestTemplate.method();
            requestTemplate.header("appId",apiAppId);
            requestTemplate.header("secret",apiAppSecret);
            if ("POST".equals(method)) {
                Request.Body body = requestTemplate.requestBody();
                String jsonLine = body.asString();
                JSONObject jsonObject = JSON.parseObject(jsonLine);
                Map<String, Object> map = new HashMap<>();
                jsonObject.forEach(map::put);
                map.put("client_id", apiAppId);
                Request.Body newBody = Request.Body.encoded(JSON.toJSONString(map).getBytes(), Charset.defaultCharset());
                requestTemplate.body(newBody);
            }
        }
    }

    @GetMapping(value = "/login/user_logout", consumes = MediaType.APPLICATION_JSON_VALUE)
    void logout(@RequestBody LogoutQuery token);

    @GetMapping(value = "/token/get_user_allInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<UserAuthorityModel> getUserMenu(@RequestBody SSOQuery query);

    @GetMapping(value = "/token/get_simple_userInfo", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<SSOSimpleUserInfo> getSimpleUserInfo(SSOQuery query);


    @GetMapping(value = "/token/valid_token", consumes = MediaType.APPLICATION_JSON_VALUE)
    FeignSSOResponseModel<Object> checkToken(@RequestBody SSOQuery query);
}
